//
//  test.m
//  CMNavBar
//
//  Created by Apple on 12/7/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "profilesViewController.h"

@implementation profilesViewController
@synthesize tableViewCell6;
@synthesize textheigth;
@synthesize TableViewCell5;
@synthesize TextAge;
@synthesize tableViewCell4;
@synthesize textBlood;
@synthesize tableViewCell3;
@synthesize textSex;
@synthesize tableViewcell2;
@synthesize tableweidth;
@synthesize tablelable;
@synthesize tableViewPt;
@synthesize textBox1;
@synthesize profileImage;
@synthesize tableViewCell1;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return 2;
    return (section == 0)? 2: 6; // **180 แบ่ง section table เป็นส่วนๆ
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //return (indexPath.section == 0 ? 90 : 44)
    //&&(indexPath.row == 0 ?  44: 44);
    return indexPath.row == 0 ? 90 : 44;
//    return indexPath.section == 2 ? 50 : 44;
//    return indexPath.section == 3 ? 50 : 44; 
    //    return indexPath.row == 4 ? 150 : 44;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    // Configure the cell.
    
//    switch (indexPath.row) {
//        case 0:
//            cell.textLabel.text = NSLocalizedString(@"Food", @"Detail");
//            
//            break;
//        case 1:
//            cell.textLabel.text = NSLocalizedString(@"Exercise", @"Detail");
//            break;
//            
//    }
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                    cell = tableViewPt;
                    break;
                case 1:
                    cell = tablelable;
                    break;
            }
            break;
 
        case 1:
            switch (indexPath.row) {
                
                case 0:
                    cell = tableViewCell1;
                    break;
                case 1:
                    cell = tableViewcell2;
                    break;
                case 2:
                    cell = tableViewCell3;
                    break;
                case 3:
                    cell = tableViewCell4;
                    break;
                case 4:
                    cell = TableViewCell5;
                    break;
                case 5:
                    cell = tableViewCell6;
                    break;
                
            }   
            break;

    }
    
    
    return cell;
}






- (void)viewDidUnload
{
    [self setProfileImage:nil];
    [self setTextBox1:nil];
    [self setTableViewCell1:nil];
    [self setTableViewPt:nil];
    [self setTablelable:nil];
    [self setTableweidth:nil];
    [self setTableViewcell2:nil];
    [self setTextSex:nil];
    [self setTableViewCell3:nil];
    [self setTextBlood:nil];
    [self setTableViewCell4:nil];
    [self setTextAge:nil];
    [self setTableViewCell5:nil];
    [self setTextheigth:nil];
    [self setTableViewCell6:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)tapProfileimg:(id)sender {
    UIActionSheet *styleAlert = [[UIActionSheet alloc] initWithTitle:@"Choose Pictures" 																												
                                                            delegate:self cancelButtonTitle:@"Cancel" 
                                              destructiveButtonTitle:nil 
                                                   otherButtonTitles:
                                 @"From Gallery",@"Take Photo", nil];
    

	
	//styleAlert.actionSheetStyle = self.navigationController.navigationBar.barStyle;
	[styleAlert showInView:self.view];
	[styleAlert release];
}


#pragma mark -
#pragma mark UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)modalView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	// Change the navigation bar style, also make the status bar match with it
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
	switch (buttonIndex)
	{
            
		case 0:
		{
			//[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
			//self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            [self presentModalViewController:picker animated:YES];
			break;
		}
		case 1:
		{
			//[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
			//self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentModalViewController:picker animated:YES];
			break;
		}
		
	}
}
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//	[picker dismissModalViewControllerAnimated:YES];
////	tapProfileimg.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
////    [profileImage setBackgroundImage:[UIImage imageWithData:[info objectForKey:@"UIImagePickerControllerOriginalImage"]] forState:UIControlStateNormal];
//    
//
//        NSData *imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1);
//    UIImage *img =[[UIImage alloc] initWithData:imgData];
//    [profileImage setBackgroundImage:img forState:UIControlEventValueChanged];
//}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    double compressionRatio=1;
    NSData *imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);
    while ([imgData length]>50000) { 
        compressionRatio=compressionRatio*0.5;
        imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);
    }
    UIImage *img=[[UIImage alloc] initWithData:imgData];
//    facImgView.image=img;
    [profileImage setBackgroundImage:img forState:UIControlStateNormal];
    NSLog(@"%@",[info objectForKey:@"UIImagePickerControllerOriginalImage"],3);//stackoverflow.com/questions/1445909/compressing-down-size-resize-image-while-storing-image-in-iphone-application
    [img release];
    [picker dismissModalViewControllerAnimated:YES];
    
//    
//    NSString *str=[NSString stringWithFormat:@"%i.jpg",[currentFaculty facultyNo]];
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *path = [NSString stringWithFormat:@"%@", [paths objectAtIndex:0]];
//    path=[path stringByDeletingLastPathComponent];
//    path=[NSString stringWithFormat:@"%@/%@",path,str];
//    [imgData writeToFile:path atomically:YES];
//    [picker dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {
    [profileImage release];
    [textBox1 release];
    [tableViewCell1 release];
    [tableViewPt release];
    [tablelable release];
    [tableweidth release];
    [tableViewcell2 release];
    [textSex release];
    [tableViewCell3 release];
    [textBlood release];
    [tableViewCell4 release];
    [TextAge release];
    [TableViewCell5 release];
    [textheigth release];
    [tableViewCell6 release];
    [super dealloc];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [textBox1 resignFirstResponder];
    [TextAge resignFirstResponder];
    [textBlood resignFirstResponder];
    [textheigth resignFirstResponder];
    [textSex resignFirstResponder];
    [tableweidth resignFirstResponder];
    

}
@end
